home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / dvips / download.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-02  |  4.5 KB  |  192 lines

  1. /*
  2.  *   Code to download a font definition at the beginning of a section.
  3.  */
  4. #include "structures.h" /* The copyright notice in that file is included too! */
  5. #ifdef SYSV
  6. #include <string.h>
  7. #else
  8. #include <strings.h>
  9. #endif
  10. /*
  11.  *   These are the external routines we call.
  12.  */
  13. extern int free() ;
  14. extern void numout() ;
  15. extern void mhexout() ;
  16. extern void cmdout() ;
  17. extern long unpack() ;
  18. extern void flip() ;
  19. extern void specialout() ;
  20. extern long getlong() ;
  21. extern void error() ;
  22. extern char *nextstring ;
  23. /*
  24.  *   These are the external variables we access.
  25.  */
  26. extern FILE *bitfile ;
  27. extern fontdesctype *curfnt ;
  28. extern long bytesleft ;
  29. extern quarterword *raster ;
  30. extern Boolean compressed ;
  31. extern integer mag ;
  32. /*
  33.  *   We might use malloc here.
  34.  */
  35. extern char *malloc() ;
  36. /*
  37.  *   We have a routine that downloads an individual character.
  38.  */
  39. void downchar(c, cc)
  40. chardesctype *c ;
  41. shalfword cc ;
  42. {
  43.    register long i, j ;
  44.    register halfword cheight, cwidth ;
  45.    register long k ;
  46.    register quarterword *p ;
  47.    register halfword cmd ;
  48.    register shalfword xoff, yoff ;
  49.    halfword wwidth ;
  50.    register long len ;
  51.  
  52.    p = c->packptr ;
  53.    cmd = *p++ ;
  54.    if (cmd & 4) {
  55.       if ((cmd & 7) == 7) {
  56.          cwidth = getlong(p) ;
  57.          cheight = getlong(p + 4) ;
  58.          xoff = getlong(p + 8) ;
  59.          yoff = getlong(p + 12) ;
  60.          p += 16 ;
  61.       } else {
  62.          cwidth = p[0] * 256 + p[1] ;
  63.          cheight = p[2] * 256 + p[3] ;
  64.          xoff = p[4] * 256 + p[5] ; /* N.B.: xoff, yoff are signed halfwords */
  65.          yoff = p[6] * 256 + p[7] ;
  66.          p += 8 ;
  67.       }
  68.    } else {
  69.       cwidth = *p++ ;
  70.       cheight = *p++ ;
  71.       xoff = *p++ ;
  72.       yoff = *p++ ;
  73.       if (xoff > 127)
  74.          xoff -= 256 ;
  75.       if (yoff > 127)
  76.          yoff -= 256 ;
  77.    }
  78.    if (compressed) {
  79.       len = getlong(p) ;
  80.       p += 4 ;
  81.    } else {
  82.       wwidth = (cwidth + 15) / 16 ;
  83.       i = 2 * cheight * (long)wwidth ;
  84.       if (i <= 0)
  85.          i = 2 ;
  86.       if (bytesleft < i) {
  87.          if (bytesleft >= RASTERCHUNK)
  88.             (void) free((char *) raster) ;
  89.          if (RASTERCHUNK > i) {
  90.             raster = (quarterword *)malloc(RASTERCHUNK) ;
  91.             bytesleft = RASTERCHUNK ;
  92.          } else {
  93.             raster = (quarterword *)malloc((unsigned)i) ;
  94.             bytesleft = i ;
  95.          }
  96.          if (raster == NULL) {
  97.             error("! out of memory during allocation") ;
  98.          }
  99.       }
  100.       k = i;
  101.       while (k > 0)
  102.          raster[--k] = 0 ;
  103.       unpack(p, raster, cwidth, cheight, cmd) ;
  104.       len = i ;
  105.    }
  106.    if (cheight == 0 || cwidth == 0 || len == 0) {
  107.       cwidth = 1 ;
  108.       cheight = 1 ;
  109.       len = 1 ;
  110.       raster[0] = 0 ;
  111.    }
  112. /*
  113.  *   Now we actually send out the data.
  114.  */
  115.    specialout('[') ;
  116.    specialout('<') ;
  117.    if (compressed)
  118.       mhexout(p, len) ;
  119.    else {
  120.       p = raster ;
  121.       i = (cwidth + 7) / 8 ;
  122.       for (j=0; j<cheight; j++) {
  123.          mhexout(p, i) ;
  124.          p += 2*wwidth ;
  125.       }
  126.    }
  127.    specialout('>') ;
  128.    numout((integer)cwidth) ;
  129.    numout((integer)cheight) ;
  130.    numout((integer)(- xoff)) ;
  131.    numout((integer)(cheight - yoff - 1)) ;
  132.    numout((integer)(c->pixelwidth)) ;
  133.    specialout(']') ;
  134.    numout((integer)cc) ;
  135.    cmdout("dc") ;
  136. }
  137. /*
  138.  *   And the download procedure.
  139.  */
  140. void download(p, psfont)
  141. charusetype *p ;
  142. int psfont ;
  143. {
  144.    register halfword b, bit ;
  145.    register chardesctype *c ;
  146.    int cc ;
  147.    char name[10] ;
  148.  
  149.    if (psfont < 27)
  150.       (void)sprintf(name, "/f%c", 'a'+psfont-1) ;
  151.    else
  152.       (void)sprintf(name, "/f%d", psfont-27) ;
  153.    cmdout(name) ;
  154.    curfnt = p->fd ;
  155.    curfnt->psname = psfont ;
  156.    if (curfnt->resfont) {
  157.       cmdout("[") ;
  158.       c = curfnt->chardesc ;
  159.       for (cc=0; cc<256; cc++, c++)
  160.          numout((integer)c->pixelwidth) ;
  161.       cmdout("]") ;
  162.       if (curfnt->resfont->specialinstructions) {
  163.             cmdout(curfnt->resfont->specialinstructions) ;
  164.       } else {
  165.       (void)strcpy(nextstring, "/") ;
  166.       (void)strcat(nextstring, curfnt->resfont->PSname) ;
  167.       cmdout(nextstring) ;
  168.       }
  169.       (void)sprintf(nextstring, "%ld", mag) ;
  170.       cmdout(nextstring) ;
  171.       (void)sprintf(nextstring, "%ld", curfnt->scaledsize) ;
  172.       cmdout(nextstring) ;
  173.       cmdout("rf") ;
  174.       return ;
  175.    }
  176.    cmdout("df") ;
  177.    c = curfnt->chardesc ;
  178.    cc = 0 ;
  179.    for (b=0; b<16; b++) {
  180.       for (bit=32768; bit>0; bit>>=1) {
  181.          if (p->bitmap[b] & bit) {
  182.             downchar(c, cc) ;
  183.             c->flags |= EXISTS ;
  184.          } else
  185.             c->flags &= ~EXISTS ;
  186.          c++ ;
  187.          cc++ ;
  188.       }
  189.    }
  190.    cmdout("dfe") ;
  191. }
  192.